package com.chunfen.wx.wa.config;

import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;
import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.chunfen.wx.wa.domain.BtTemplate;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Configuration
//@EnableWebMvc
public class WebMvcConfig{

    /**
     * 分页, 分表插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptorMysql() {

        // 分页
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType("mysql");


        DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();

        HashMap<String, ITableNameHandler> tableNameHandlerHashMap = new HashMap<>();
        // bt_template_分表

        // 不同的 分表规则 可设置 不同 的 ITableNameHandler
        tableNameHandlerHashMap.put("bt_template_", new ITableNameHandler() {
            @Override
            public String dynamicTableName(MetaObject metaObject, String sql, String tableName) {
                // ((StatementHandler)metaObject.getOriginalObject()).getParameterHandler().getParameterObject()
                // meta.getValue("delegate.boundSql.parameterObject")
                if(metaObject.hasGetter("delegate.boundSql.parameterObject")){
                    Object origObj = metaObject.getValue("delegate.boundSql.parameterObject");
                    if(origObj instanceof Map){
                        BtTemplate btTemplate  = (BtTemplate) ((Map) origObj).get("bt");
                        System.out.println("dubbo param bt " + btTemplate);
                        return tableName + "1";
                    } else if(origObj instanceof BtTemplate){
                        BtTemplate btTemplate  = (BtTemplate) origObj;
                        System.out.println("singial param bt " + btTemplate);
                        return tableName + "2";
                    }
                }
                return tableName;
            }
        });

        // 分表
        dynamicTableNameParser.setTableNameHandlerMap(tableNameHandlerHashMap);
        paginationInterceptor.setSqlParserList(Collections.singletonList(dynamicTableNameParser));

        return paginationInterceptor;
    }



//    @Bean
//    public SpringResourceTemplateResolver templateResolver(){
//        // SpringResourceTemplateResolver automatically integrates with Spring's own
//        // resource resolution infrastructure, which is highly recommended.
//
//        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
//        templateResolver.setPrefix("classpath:/templates/");
//        templateResolver.setSuffix(".html");

//        // HTML is the default value, added here for the sake of clarity.
//
//        templateResolver.setTemplateMode(TemplateMode.HTML);
//
//        // Template cache is true by default. Set to false if you want
//        // templates to be automatically updated when modified.
//
//        templateResolver.setCacheable(true);
//        return templateResolver;
//    }
//
//    @Bean
//    public SpringTemplateEngine templateEngine(){
//        // SpringTemplateEngine automatically applies SpringStandardDialect and
//        // enables Spring's own MessageSource message resolution mechanisms.
//
//        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
//        templateEngine.setTemplateResolver(templateResolver());
//
//        // Enabling the SpringEL compiler with Spring 4.2.4 or newer can
//        // speed up execution in most scenarios, but might be incompatible
//        // with specific cases when expressions in one template are reused
//        // across different data types, so this flag is "false" by default
//        // for safer backwards compatibility.
//
//        templateEngine.setEnableSpringELCompiler(true);
//        return templateEngine;
//    }
//
//    @Bean
//    public ThymeleafViewResolver viewResolver(){
//        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
//        viewResolver.setTemplateEngine(templateEngine());
//        return viewResolver;
//    }

}
